package view;


import java.awt.Font;

import mdes.slick.sui.Button;
import mdes.slick.sui.Container;
import mdes.slick.sui.Display;
import mdes.slick.sui.event.ActionEvent;
import mdes.slick.sui.event.ActionListener;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

import controller.MisterJumpController;
import model.Item;
import model.Viewport;
import model.Wrapper;


/*
 *  Gameplay state = when user presses "play", this code will launch and here we play     
 * 	Missing drawing method(), setFPS(), getFPS()
 */
public class MisterJumpGameplay extends BasicGameState {
	private int stateID = -1;

	private boolean pauseButtonPressed = false;
	private static Wrapper modelWrapper = null;
	private static MisterJumpController controller = null;
	private Input misterInput = null;
	private int points = 0;

	//font
	String fontPath = "/data/VTKSAnimal2.ttf";
	UnicodeFont font;
	 // Image loading variable declerations
	private Image background = null;
	private Image MisterLaw = null;	
	
	public MisterJumpGameplay(int stateID) {
		this.stateID = stateID;
	}
	
	@Override
	public void init(GameContainer container, StateBasedGame game)
			throws SlickException {
		background = new Image("data/background.png");
		MisterLaw = new Image("/data/misterjump.png");
		
		//font
		font = new UnicodeFont(fontPath, 20, false, false);
		font.addAsciiGlyphs();  
		font.addGlyphs(400, 600);
		font.getEffects().add(new ColorEffect(java.awt.Color.BLACK));
		font.loadGlyphs();  
		
        Container content = new Container();
        content.setSize(320, 512);
        content.setLocation(0, 0); 
        content.setOpaque(false); 
		        
		modelWrapper = Wrapper.getInstance();
		controller = new MisterJumpController();
		
	}

	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
		// TODO Auto-generated method stub
		// fix me!
		background.draw(0,0);
		font.drawString(100, 10, String.valueOf(points));
		int w = Viewport.WIDTH;
		int h = Viewport.HEIGHT;
		
		Item[] things = modelWrapper.holeSichtfenster();
		for (int i = 0; i < things.length; i++)
		{
			Image img = things[i].getImage();
			img.draw((float)things[i].getX() * w, (float)(1-things[i].getY()-things[i].getHeight()) * h, (float)things[i].getWidth() * w, (float)things[i].getHeight() * h);
		
		}
		
		MisterLaw.draw((float)modelWrapper.getMisterX()*Viewport.WIDTH, (float)(1-modelWrapper.getMisterY()-modelWrapper.getMisterHeight())*Viewport.HEIGHT, (float)(Viewport.WIDTH*modelWrapper.getMisterWidth()), (float)(Viewport.HEIGHT*modelWrapper.getMisterHeight()));
	}

	@Override
	public void update(GameContainer container, StateBasedGame game, int delta)
			throws SlickException {
		// TODO Auto-generated method stub
		points = (int)(modelWrapper.getPoints()*100);
		misterInput = container.getInput();
		
		if(misterInput.isKeyDown(Input.KEY_LEFT)){
			controller.leftKeyPressed(true);
		}
		if(misterInput.isKeyDown(Input.KEY_RIGHT)){
			controller.rightKeyPressed(true);
   		}
       	if (misterInput.isKeyDown(Input.KEY_SPACE)) {
       		Sound shootSound = new Sound("/data/shoot.wav");
       		shootSound.play();
       		controller.shoot(0);
       	}
       	if (modelWrapper.isMisterAlive() == false)
		{
       		Sound die = new Sound("data/die.wav");
       		die.play();
       		game.enterState(MisterJumpMain.GAMEOVERSTATE,new FadeOutTransition(),new FadeInTransition());
		}
		else if (pauseButtonPressed) {
			System.out.print("Pussy");
			pauseButtonPressed = false;
		}
		
		controller.act();
	}
	
	public int  getID() {
		return stateID;
	}
}
